﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text;
using System.Collections.Generic;

namespace Jy123.Web.Album
{
    public partial class PhotoFriendComment : System.Web.UI.Page
    {
        private int userID ;//暂用用户ID，写死，到时候用COOKIE替换过来
        int userAccess = 1;//访问者ID
        //int albumID = 9;//暂用相册，到时候点击所属相册时传进来
        //protected int photoID = 69;//暂用相片的ID，到时候点击所属相册时传进来
        //int CurrentPhotoNum = 3;//？？？？？？？？？？？？？第几张照片
        protected void Page_Load(object sender, EventArgs e)
        {
            string strUserIdFromCookie = Common.Cookie.getCookie("UserID");
            if (string.IsNullOrEmpty(strUserIdFromCookie))
            {
                Response.Redirect("/");
            }
            userID = int.Parse(strUserIdFromCookie);
            userAccess= int.Parse(strUserIdFromCookie);

            string strAlbumID = Request.QueryString["AlbumID"];
            string strPhotoID = Request.QueryString["PhotoID"];
            int albumID = 0;
            int photoID = 0;
            if (!string.IsNullOrEmpty(strAlbumID))
            {
                albumID = Convert.ToInt32(strAlbumID);
                //hidAlbumID =Convert.ToInt32(strAlbumID);
            }
            else
            {
                strAlbumID = Request.Form["hidAlbumID"];
                albumID = int.Parse(strAlbumID);
            }



            if (!string.IsNullOrEmpty(strPhotoID))
            {
                photoID = Convert.ToInt32(strPhotoID);
            }
            else
            {
                strPhotoID = Request.Form["hidPhotoID"];
                photoID = int.Parse(strPhotoID);
            }

            string type = Request.QueryString["Type"];
            if (type == "del")//删除
            {
                new BLL.Photo_Comment().Delete(Convert.ToInt32(Request.QueryString["id"]));
            }
            if (type == "delPhoto")
            {
                new BLL.User_Photo().Delete(photoID);
                Response.Redirect("Index.aspx");
            }
            if (Request.Form["comment"] == "评论")
            {
                Jy123.Model.Photo_Comment mPhotoComment = new Jy123.Model.Photo_Comment();
                Jy123.BLL.Photo_Comment bllPhotoComment = new Jy123.BLL.Photo_Comment();
                mPhotoComment.FPhoto = photoID;
                mPhotoComment.FUser = userAccess;
                mPhotoComment.IsLock = false;
                mPhotoComment.PostIP = Common.Public.GetClientIP(); ;
                mPhotoComment.PostTime = DateTime.Now;
                mPhotoComment.CommContent = Request.Form["txtcomment"];
                bllPhotoComment.Add(mPhotoComment);
            }

            #region 公共部分
            StringBuilder strHtml = new StringBuilder(Cache["user_photoFriendcomment"].ToString());
            strHtml = strHtml.Replace("$Header$", Cache["user_header2"].ToString());
            strHtml = strHtml.Replace("$Footer$", Cache["user_footer"].ToString());
            strHtml = strHtml.Replace("$LeftUserCmd$", Cache["user_navigation_down"].ToString());
            strHtml = strHtml.Replace("$LeftUserFriendAdd$", Cache["user_friendadd"].ToString());

            //统计站内信数量
            Jy123.BLL.User.Letter bllLetter = new Jy123.BLL.User.Letter();
            int newLetterCount = bllLetter.CountNewLetter(userID);
            strHtml = strHtml.Replace("$NewMsg$", newLetterCount.ToString());
            #endregion

            #region 相片
            Model.User_Album modelAlbum = new BLL.User_Album().GetModelByCache(albumID);
            Model.User_Photo modelPhoto = new BLL.User_Photo().GetModel(photoID);

            List<int?> count; int lastID = 0; int nextID = 0;
            //DataTable dtPhotoList = new BLL.User.Album().Photo(1,1,"FAlbum='"+albumID+"'","ID asc",out count).Tables[0];

            Model.User_Main modelUser = new BLL.User_Main().GetModel(userAccess);

            DataTable dtPhotos = new BLL.User.Album().Photo(1, int.MaxValue, "FAlbum='" + albumID + "'", "ID DESC",out count).Tables[0];//new BLL.User_Photo().GetList("FAlbum='" + albumID + "'").Tables[0]; //new BLL.User.Album().PhotoList(albumID, "ID asc", 1,int.MaxValue).Tables[0];
            dtPhotos.PrimaryKey = new DataColumn[] { dtPhotos.Columns["ID"] }; //手动设置主键！
            DataTable dtPhoto = new BLL.User.Album().ViewPhoto(albumID, photoID, out lastID, out nextID).Tables[0];

            modelPhoto.Views += 1;//查看次数加1

            new BLL.User_Photo().Update(modelPhoto);
            string last = @"<a class=""up_a"" href=""/Album/PhotoFriendComment.aspx?AlbumID=$AlbumID$&PhotoID=$UpPhotoID$"">上一张</a>";
            string next = @"<a class=""down_a"" href=""/Album/PhotoFriendComment.aspx?AlbumID=$AlbumID$&PhotoID=$NextPhotoID$"">下一张</a>";
            string nextPhoto = "/Album/PhotoFriendComment.aspx?AlbumID=$AlbumID$&PhotoID=$NextPhotoID$";
            //string strPhotoComment = Cache["user_photocomment"].ToString();
            StringBuilder sbPhotoComment = new StringBuilder();

            strHtml = strHtml.Replace("$CurrentNum$", (dtPhotos.Rows.IndexOf(dtPhotos.Rows.Find(photoID))+1).ToString());

            //判断用户是否有头像
            modelUser.Portrait = new Jy123.BLL.User.SPace().DefaultPortrait(userID);

            strHtml = strHtml.Replace("$ImagesCount$", dtPhotos.Rows.Count.ToString())//modelAlbum.ImagesCount.ToString())                
                .Replace("$FilePath$", modelPhoto.FilePath)
                .Replace("$Description$", modelPhoto.Description)
                .Replace("$PhotoPostTime$", modelPhoto.PostTime.ToString())
                .Replace("$AlbumTitle$", modelAlbum.Title)
                .Replace("$Comments$", new BLL.User.Album().PhotoCommentCount(photoID).ToString())//modelPhoto.Comments.ToString())
                .Replace("$Views$", modelPhoto.Views.ToString())
                .Replace("$UserName$", modelUser.UserName)
                .Replace("$Portrait$", modelUser.Portrait)
                .Replace("$last$", (lastID > 0) ? last : "")
                .Replace("$next$", (nextID > 0) ? next : "")
                .Replace("$nextphoto$", (nextID > 0) ? nextPhoto : "#")
                .Replace("$UpPhotoID$", lastID.ToString())
                .Replace("$NextPhotoID$", nextID.ToString());
            #endregion
            #region 评论列表
            StringBuilder sbCommentli = new StringBuilder();
            string strCommentli = Cache["user_photomy_comment_li"].ToString();
            DataTable dtComment = new BLL.User.Album().PhotoCommentList(1, int.MaxValue, "FPhoto='" + photoID + "'").Tables[0];
            string strDela = @"<a href=""javascript:;"" class=""imgdel"" url=""/Album/PhotoFriendComment.aspx?Type=del&AlbumID=$AlbumID$&PhotoID=$PhotoID$&CommentID=$ID$"">
        <img src=""/images/del_blog.gif"" alt=""删除"" /></a>";
            for (int i = 0; i < dtComment.Rows.Count; i++)
            {
                sbCommentli.Append(strCommentli);
                if (dtComment.Rows[i]["FUser"].ToString().Equals(userAccess.ToString()))
                {
                    sbCommentli = sbCommentli.Replace("$imgdel$", strDela);
                }
                else
                {
                    sbCommentli = sbCommentli.Replace("$imgdel$", "");
                }
                //根据用户ID判断用户是否有头像,如果没有头像就返回默认头像地址
                string userPortrait = new Jy123.BLL.User.SPace().DefaultPortrait(Convert.ToInt32(dtComment.Rows[i]["FUser"]));

                sbCommentli = sbCommentli.Replace("$UserName$", dtComment.Rows[i]["UserName"].ToString())
                    .Replace("$PostTime$", dtComment.Rows[i]["PostTime"].ToString())
                    .Replace("$ID$", dtComment.Rows[i]["ID"].ToString())
                    .Replace("$Portrait$", dtComment.Rows[i]["Portrait"].ToString())
                    .Replace("$CommContent$", dtComment.Rows[i]["CommContent"].ToString())
                    .Replace("PhotoID=$PhotoID$&", string.Empty);

                //sbCommentli.Append(strCommentli);
            }
            #endregion
            strHtml = strHtml.Replace("$PhotoMyCommentli$", sbCommentli.ToString());
            strHtml = strHtml.Replace("$hidAlbumIDValue$", albumID.ToString());
            strHtml = strHtml.Replace("$hidPhotoIDValue$", photoID.ToString());
            strHtml = strHtml.Replace("$AlbumID$", albumID.ToString());
            //strHtml = strHtml.Replace("$NextAlbumID$", (albumID+1).ToString());
            //sbAlbumList.Append(strPhotoComment);
            //strHtml.Append(sbAlbumList.ToString());
            Response.Write(strHtml);
        }
    }
}
